home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / LogLibrary 950622 / Src / LogFormat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-20  |  5.2 KB  |  233 lines  |  [TEXT/MPCC]

  1. /*                                        LogFormat.c                                    */
  2. /*
  3.  * LogFormat.c
  4.  * Copyright © 1992-94 Apple Computer Inc. All Rights Reserved.
  5.  * Programmed by Martin Minow,
  6.  *    Internet:    minow@apple.com
  7.  *    AppleLink:    MINOW
  8.  *
  9.  * Edit History
  10.  *    93.01.09 MM        First public release
  11.  *    93.07.09 MM        Reformatted for 80-column page. No substantive changes.
  12.  *    94.12.10 MM        Redone (form AuditEntryFormat) for the Driver Log.
  13.  *
  14.  * Format a LogRecordEntry. Note: there is no size checking, which means that if you
  15.  * somehow manage to create an extra-long record, you'll get garbage. The local
  16.  * formatting functions won't corrupt memory, however. result may not be an expression
  17.  * with side-effects.
  18.  */
  19. #include "LogLibrary.h"
  20.  
  21. #define AppendChar(result, theChar)    do {                \
  22.         StringPtr        _dst = (result);                \
  23.         _dst[++_dst[0] & 0xFF] = theChar;                \
  24.     } while (0)
  25. static void                    AppendUnsignedLeadingZeros(
  26.         StringPtr                result,
  27.         unsigned long            value,
  28.         short                    digits,
  29.         short                    terminator
  30.         
  31.     );
  32. static void                    AppendSigned(
  33.         StringPtr                result,
  34.         signed long                value
  35.     );
  36. static void                    AppendUnsigned(
  37.         StringPtr                result,
  38.         unsigned long            value
  39.     );
  40. static void                    AppendHexLeadingZeros(
  41.         StringPtr                result,
  42.         unsigned long            value,
  43.         short                    digits
  44.     );
  45. static void                    AppendOSType(
  46.         StringPtr                result,
  47.         OSType                    datum
  48.     );
  49. static void                    AppendPascalString(
  50.         StringPtr                result,
  51.         const StringPtr            datum
  52.     );
  53.  
  54. enum {
  55.     NUL            = 0,
  56.     kOpenQuote    = 0xD2,
  57.     kCloseQuote    = 0xD3
  58. };
  59.  
  60.  
  61. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  62.  * FormatLogEntryData
  63.  *
  64.  * This function is in LogFormat.c - it formats an entry into a single line
  65.  * that is stored in the result. Note: only the data is formatted: the timestamp
  66.  * is not processed. This function may be called from a non-application context;
  67.  * It does not require a MixedMode switch.
  68.  */
  69. pascal void
  70. FormatLogEntryData(
  71.         const LogEntryPtr        thisLogEntry,            /* Format this entry        */
  72.         StringPtr                result
  73.     )
  74. {
  75.         UInt32                    datum;
  76.         UInt32                    format;
  77.         unsigned int            thisFormat;
  78.         register UInt32            *dataPtr;
  79. #define ENTRY    (*thisLogEntry)
  80.         
  81.         result[0] = 0;
  82.         AppendOSType(result, ENTRY.idCode);
  83.         AppendPascalString(result, "\p: ");
  84.         format = ENTRY.format;
  85.         dataPtr = ENTRY.data;
  86.         while (dataPtr < &ENTRY.data[kLogEntryDataSize]) {
  87.             thisFormat = (format & kLogFormatMask);
  88.             format >>= kLogFormatShift;
  89.             if (thisFormat == kLogFormatEnd)
  90.                 break;
  91.             else if (thisFormat == kLogFormatString) {
  92.                 if (dataPtr > &ENTRY.data[0])
  93.                     AppendChar(result, ' ');
  94.                 AppendChar(result, kOpenQuote);            /* Open double quote    */
  95.                 AppendPascalString(result, (StringPtr) dataPtr);
  96.                 AppendChar(result, kCloseQuote);
  97.                 break;                                    /* Exit loop            */
  98.             }
  99.             else {
  100.                 if (dataPtr > &ENTRY.data[0])
  101.                     AppendPascalString(result, "\p, ");
  102.                 datum = *dataPtr++;
  103.                 switch (thisFormat) {
  104.                 case kLogFormatSigned:
  105.                     AppendSigned(result, (signed long) datum);
  106.                     break;
  107.                 case kLogFormatUnsigned:
  108.                     AppendUnsigned(result, datum);
  109.                     break;
  110.                 case kLogFormatHex:
  111.                     AppendHexLeadingZeros(result, datum, sizeof (UInt32) * 2);
  112.                     AppendPascalString(result, "\p=");
  113.                     AppendOSType(result, datum);
  114.                     break;
  115.                 case kLogFormatAddress:
  116.                 default:                        /* Unknown        */
  117.                     AppendHexLeadingZeros(result, datum, sizeof (UInt32) * 2);
  118.                     break;
  119.                 }
  120.             }
  121.         }
  122. #undef ENTRY
  123. }
  124.  
  125. /*
  126.  * Output an n-digit decimal value with leading zeros.
  127.  */
  128. static void
  129. AppendUnsignedLeadingZeros(
  130.         StringPtr                result,
  131.         unsigned long            value,
  132.         short                    digits,
  133.         short                    terminator
  134.     )
  135. {
  136.         if (--digits > 0)
  137.             AppendUnsignedLeadingZeros(result, value / 10, digits, NUL);
  138.         AppendChar(result, (value % 10) + '0');
  139.         if (terminator != NUL)
  140.             AppendChar(result, terminator);
  141. }
  142.  
  143. /*
  144.  * Output a signed decimal longword.
  145.  */
  146. static void
  147. AppendSigned(
  148.         StringPtr                result,
  149.         signed long                value
  150.     )
  151. {
  152.         if (value < 0) {
  153.             AppendChar(result, '-');
  154.             value = (-value);
  155.         }
  156.         AppendUnsigned(result, (unsigned long) value);
  157. }
  158.  
  159. /*
  160.  * Output an unsigned decimal longword.
  161.  */
  162. static void
  163. AppendUnsigned(
  164.         StringPtr                result,
  165.         unsigned long            value
  166.     )
  167. {
  168.         if (value >= 10)
  169.             AppendUnsigned(result, value / 10);
  170.         AppendChar(result, (value % 10) + '0');
  171. }
  172.  
  173. /*
  174.  * Output a string of hex digits with leading zeros.
  175.  */
  176. static void
  177. AppendHexLeadingZeros(
  178.         StringPtr                result,
  179.         unsigned long            value,
  180.         short                    digits
  181.     )
  182. {
  183.         if (--digits > 0)
  184.             AppendHexLeadingZeros(result, value >> 4, digits);
  185.         value &= 0x0F;
  186.         AppendChar(result,
  187.                 (value < 10)
  188.                 ? value + '0'
  189.                 : (value + ('A' - 10))
  190.             );
  191. }
  192.  
  193. /*
  194.  * Output a 4-byte character string. Unknown
  195.  * bytes (characters outside the range ' ' to '~')
  196.  * are replaced as '.'.
  197.  */
  198. static void
  199. AppendOSType(
  200.         StringPtr                result,
  201.         OSType                    datum
  202.     )
  203. {
  204.         register short            i;
  205.         register unsigned char    c;
  206.         union {
  207.             OSType                osType;
  208.             unsigned char        c[sizeof (OSType)];
  209.         } value;
  210.         
  211.         AppendChar(result, '\'');
  212.         value.osType = datum;
  213.         for (i = 0; i < sizeof (OSType); i++) {
  214.             c = value.c[i];
  215.             if (c < ' ' || c >= 0x7F)
  216.                 c = '.';
  217.             AppendChar(result, c);
  218.         }
  219.         AppendChar(result, '\'');
  220. }
  221.  
  222. static void
  223. AppendPascalString(
  224.         StringPtr                result,
  225.         const StringPtr            datum
  226.     )
  227. {
  228.         register short            i;
  229.         
  230.         for (i = 1; i <= datum[0]; i++)
  231.             AppendChar(result, datum[i]);
  232. }
  233.